﻿#ifndef TOOL_H
#define TOOL_H

#include <QtGlobal>
#include <QString>
#include <QStringList>
#include <QList>
#include <QDate>
#include <QTime>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QImage>
#include <QPixmap>
#include <string.h>
#include <QLabel>
#include <QColor>
#include <QPalette>
#include <iostream>
#include <fstream>
#include <QRegExp>
#include <QCryptographicHash>
#include <QGuiApplication>
#include <QScreen>

#include <opencv2/opencv.hpp>
#include <opencv2/photo/photo.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#pragma execution_character_set("utf-8")

#define SCREENWIDTH1K 1920
#define SCREENWIDTH2K 2560
#define SCREENWIDTH4K 3840

void setLog(bool log);
QString getCurPath(void);
bool containsChinese(const QString str);
QString getDirName(const QString format);
QString getPathName(const QString path,const QString name);

int getIntToMark(int mark);
void setSplitterListSize(const QString strSize, QList<int> &sizes);
void setSplitterSizeStr(const QList<int> sizes, QString &strSize);

void setLabelRGB(QLabel *label,int r,int g,int b);
QString getRGB(int r,int g,int b);
QString getRed(void);

int getScreenNums(void);
void getSysScreen(void);
bool winIsScreen(int x,int width);
bool is4KScreen(void);
void set4KScreen(int x);
QString getComboBoxSheet(int h);
QString getNumber(const float N,const int dec=3);
bool saveImageEx(const QString name,const int Dpi=300, const int Rotate=0, QImage *img = nullptr);
void setLogPath(const QString path);
void retInfo(const QString msg);
void saveError(const QString msg, int err, int ok=1);
void saveLogName(const QString msg,const QString fname);
void saveLog(const QString msg);
void qStrToCstr(const QString q_str,char *v_str,size_t size);
int qStrToInt(const QString q_str);
float qStrToFloat(const QString q_str);
void qStrToInts(const QString q_str,int *v_ints,size_t size);
QString renameTemp(const QString name,int count);
void initRand(void);

void getFilename(const QString name, int (&no)[2]);
void setFilename(int num,QString &name,const QString dir,const QString ext,int fileLen);

cv::Mat cvRead(const QString name,const int flags);
// void cvWrite(const cv::Mat inMat, const QString name);

QString getImageInfo(const QImage::Format imgFormat);
QImage  cvMatToQImage( const cv::Mat &inMat );
QPixmap cvMatToQPixmap( const cv::Mat &inMat );
QPixmap rotatePixmap(const QPixmap &pixmap, int angleDegrees);
cv::Scalar qcolor2scalar(QColor color);
void cvMatImageSave(const cv::Mat inMat, const QString name,bool isNeg,int saveFormat,int para);

void RGB2BGR( unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight);
// int  getMvsType(int nValue);
// int  getMvsValue(int nType);
// cv::Mat imageMvsToMat(unsigned char* pImageData, int nWidth, int nHeight, int nValue);
int imageKsjToMat(unsigned char* pImageData, int nWidth, int nHeight, int nBitCount, cv::Mat **image);
cv::Mat imageKsjToMat(unsigned char* pImageData, int nWidth, int nHeight, int nBitCount);
cv::Mat matResize(const cv::Mat &image, int nWidth, int nHeight, float &scale);
cv::Mat matResize1(const cv::Mat &image, int nWidth, int nHeight);
float imageScale(int nWidth, int nHeight,int sWidth, int sHeight);
float imageScale1(int n,int s);
bool rectInRect(const cv::Rect &rectIn,const cv::Rect &rectOut);
void sortBox(const QVector<cv::Rect> inBox, QVector<cv::Rect> &outBox);
cv::Rect boxScale(const cv::Rect box,float scale);
bool isInBox(const cv::Rect box, QVector<cv::Rect> boxs);
float getAngle(const cv::Size size);
float getPointAngle2(const cv::Point2f pointO, const cv::Point2f pointA);
//float getPointToPointAngle(const cv::Point2f pts[]);
//int getPointToPointDirect(const cv::Point2f pts[]);
float imageCmpDiff(const cv::Mat image1,const cv::Mat image2);
float imageCmp(const cv::Mat image1,const cv::Mat image2);
//float imageCmpOrb(const cv::Mat image1,const cv::Mat image2);
float matLightMean(const cv::Mat image);
float nameLightMean(const QString name);
float matLightClearness(const cv::Mat image);
float matLightClearnessFlag(const cv::Mat image,int flag);
float nameImageClearness(const QString name,int flag=-1);
int getRotateValue(int index);
int getWidthHeightFormat(int width,int height,int iwidth,int iheight,float format);
bool maxInnerRect(const std::vector<cv::Point> con, cv::Rect &R);
void blackImageBox(cv::Mat &image, cv::Rect box);
int blackImageBorderBox(cv::Mat &image,const cv::Rect box,const QSize winSize,const QSize borderSize);
int getImageContoursMax(const std::vector<std::vector<cv::Point>> contours,const std::vector<cv::Vec4i>hierarchy,
                        cv::Rect &maxBox,int &maxArea,int MinWidth=-1,int MinHeight=-1);
void procImageHdr(const cv::Mat highMat,const cv::Mat normalMat,const cv::Mat lowMat,cv::Mat &hdrMat,const float w1=0.5,const float w2=0.002);
QString getFileBase64Content(const QString path);
QString getFileMd5(const QString filePath);

#endif // TOOL_H
